/* COPYRIGHT_CHUNFENG */
#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdint.h>
#include <stdlib.h>
#include <cf_string.h>
#include <sys/types.h>

#ifdef __GNUC__
#define CF_GNUC_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
#else
#define CF_GNUC_WARN_UNUSED_RESULT
#endif

#ifndef BOOL
#define BOOL int
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#define int8  char
#define uint8  unsigned char
#define int16  short
#define uint16  unsigned short
#define int32  long
#define uint32  unsigned long
#define int64  long long
#define uint64  unsigned long long

void uint32_set_bits(uint32* reg, uint32 mask);
void uint32_clear_bits(uint32* reg, uint32 mask);
void uint32_modify_bits(uint32* reg, uint32 mask, uint32 value);

#ifndef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
#endif
#endif
#define container_of(__pointer, __type, __member) ((__type*)((char*)__pointer - offsetof(__type, __member)))

#define CF_CONST_STR_LEN(__str)   __str, (sizeof(__str)-1)


void CFHalfByteToHex(char hex[1], const char halfByte);
void CFByteToHex(char hex[2], const char byte);
int CFRawToHex(char* string, void* raw, size_t rawSize);

int cf_char_is_blank(char c);
void cf_char_strip(char** start, char** end, int strip_start, int strip_end);
char* strblank(const char* str);
char* strnblank(const char* str);

int CFStrToInt(const char* str, int* value);
int CFIntToString(char* str, int val);
long long cf_atoll_n(char* a, int n);
const char* cf_lltoa(long long int num);

/* Process management */
int cf_popen(const char*  args[], int* from_stdout, int* from_errout, int* to_stdin, int* pid_out);
#define CFPopen cf_popen
int cf_system(const char* args[], CFString* stdOutput, CFString* errorOutput);
#define CFSystem    cf_system
int cf_system2(CFString* stdOutput, CFString* errorOutput, const char* arg1, ...);
#define CFSystem2    cf_system2

/* Shared memory */
void* cf_create_shm(const char* key_path, size_t size);
int cf_delete_shm(const char* key_path);
void* cf_get_shm(const char* key_path);

/* I/O routines */
int CFFdPrintf(int fd, const char *fmt, ...);
int cf_string_to_file(const char* file_path, const char* str);
#define CFStringToFile  cf_string_to_file
int cf_file_to_string(CFString* str, const char* file_path);
#define CFFileToString  cf_file_to_string
int cf_fd_to_string(CFString* str, int fd, int len);

/* Daemon routines */
int CFDaemon(const char* pidFile, int lockPort);
int CFStopDaemon(const char* pidFile);
BOOL CFIsDaemonExited(int lockPort);
int cf_get_process_path(CFString* path, pid_t pid);
int cf_get_process_dir(CFString* dir, pid_t pid);

/* Filesystem utilities */
int cf_file_to_dir(CFString* dir, const char* file);
int CFFileIsCharDevice(const char* filePath);
void cf_mkdir(const char* dir, mode_t mode);
int cf_get_disk_uuid(const char* dev, CFString* uuid_out);
int cf_gen_grub2_cfg(const char* cfg_file, char* boot_dev, char* root_dev);
#endif

